package calculus.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.BoxLayout;

import calculus.function.SingleVarFunction;
import calculus.function.exceptions.InputException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;

public class MainWindow {

	private JFrame frame;
	private JTextField inputBox = new JTextField();;

//	/**
//	 * Launch the application.
//	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					MainWindow window = new MainWindow();
//					window.frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}
	
	public void Show() {
		try {
			MainWindow window = new MainWindow();
			window.frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String GetInput() {
		return inputBox.getText();
	}
	
	public void AddGraph(int graphId, SingleVarFunction fn) {
		drawPanel.AddFunction(graphId, fn);
	}
	
	public void RemoveGraph(int graphId) {
		drawPanel.RemoveLines(graphId);
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent arg0) {
				drawPanel.AdjustZoom(arg0.getPreciseWheelRotation());
			}
		});
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		frame.getContentPane().add(inputPanel, BorderLayout.SOUTH);
		inputPanel.setLayout(new BorderLayout(0, 0));
		inputBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addGraph();
			}
		});
		
		inputPanel.add(inputBox, BorderLayout.CENTER);
		inputBox.setColumns(10);
		
		JButton graphBtn = new JButton("Graph");
		graphBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addGraph();
			}
		});
		drawPanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				mouseLastX = e.getX();
				mouseLastY = e.getY();
			}
			public void mouseClicked(MouseEvent e) {
				if(e.getButton() == MouseEvent.BUTTON1) {
					pauseInfo = !pauseInfo;
				}
			}
		});
		drawPanel.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent arg0) {
				drawPanel.AdjustOffest(arg0.getX() - mouseLastX, arg0.getY() - mouseLastY);
				mouseLastX = arg0.getX();
				mouseLastY = arg0.getY();
			}
			@Override
			public void mouseMoved(MouseEvent arg0) {
				if(!pauseInfo) {
					drawPanel.SetMouseLocation(arg0.getX());
				}
				
			}
		});

		inputPanel.add(graphBtn, BorderLayout.EAST);
		frame.getContentPane().add(drawPanel, BorderLayout.CENTER);
	}
	
	private void addGraph() {
		try {
			String input = inputBox.getText();
			SingleVarFunction svf = new SingleVarFunction(
					Processor.ProcessInput(input), "x");
			AddGraph(0, svf);
		} catch (InputException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private JGraph drawPanel = new JGraph();
	private JPanel inputPanel = new JPanel();
	private int mouseLastX;
	private int mouseLastY;
	private boolean pauseInfo = false;

}
